var csInterface = new CSInterface();

// 个人主页
var homePageElement = document.querySelector("#homePage");
homePageElement.addEventListener("click", callHomepage);

function callHomepage() {
  window.cep.util.openURLInDefaultBrowser('https://gitee.com/brzjomo');
}

// 重命名对象
// 数字-1
var runButtonNumMinus = document.querySelector("#btn-num-minus");
runButtonNumMinus.addEventListener("click", minusOne);

function minusOne() {
  var rule = /^\d+$/;
  var inputValue = document.getElementsByName("text-rename")[0].value;
  if (rule.test(inputValue) && parseInt(inputValue) > 1) {
    document.getElementsByName("text-rename")[0].value = parseInt(inputValue) - 1;
  }
}

// 数字+1
var runButtonNumPlus = document.querySelector("#btn-num-plus");
runButtonNumPlus.addEventListener("click", plusOne);

function plusOne() {
  var rule = /^\d+$/;
  var inputValue = document.getElementsByName("text-rename")[0].value;
  if (rule.test(inputValue)) {
    document.getElementsByName("text-rename")[0].value = parseInt(inputValue) + 1;
  }
}

// 重命名
var runButtonRename = document.querySelector("#btn-rename");
runButtonRename.addEventListener("click", rename);

function rename() {
  // 引入rename.jsx
  const path = csInterface.getSystemPath(SystemPath.EXTENSION);
  csInterface.evalScript(`$.evalFile("${path}/areaTool/jsx/rename.jsx")`);

  var namePrefix = document.getElementsByName("text-prefix")[0].value;
  var name = document.getElementsByName("text-rename")[0].value;
  var finalName = namePrefix + name;
  csInterface.evalScript("rename(" + '"' + finalName + '"' + ")");

  var ifAutoIncrementElement = document.getElementById("ifAutoIncrement");
  var ifAutoIncrement = ifAutoIncrementElement.checked;
  if (ifAutoIncrement) {
    plusOne();
  }
}

// 调用排序函数
var runButtonOrder = document.querySelector("#run-button-order");
runButtonOrder.addEventListener("click", order);

function order() {
  csInterface.evalScript("order()");
}

//调用面积计算函数
var runButtonCalculateArea = document.querySelector("#run-button-calculateArea");
runButtonCalculateArea.addEventListener("click", preCalculateArea);

function preCalculateArea() {
  // 引入calculateArea.jsx
  const path = csInterface.getSystemPath(SystemPath.EXTENSION);
  csInterface.evalScript(`$.evalFile("${path}/areaTool/jsx/calculateArea.jsx")`);

  // 获取宽幅
  var materialWidthElement = document.getElementById("material-width");
  var materialWidth = parseFloat(materialWidthElement.options[materialWidthElement.selectedIndex].value);

  // 获取制图比例
  var drawingScaleElement = document.getElementById("drawing-scale");
  var drawingScale = parseInt(drawingScaleElement.options[drawingScaleElement.selectedIndex].value);

  // 获取数据精度
  var dataAccuracyElement = document.getElementById("data-accuracy");
  var dataAccuracy = parseInt(dataAccuracyElement.options[dataAccuracyElement.selectedIndex].value);

  // 获取额外文本布尔值
  var extraTextElement = document.getElementById("extraText");
  var isAddExtraText = extraTextElement.checked;

  // 获取布尔-是否提示结果
  var alertResultElement = document.getElementById("isAlertResult");
  var isAlertResult = alertResultElement.checked;

  // 获取布尔-是否仅输出长度
  var outputLengthOnlyElement = document.getElementById("ifOutputLengthOnly");
  var ifOutputLengthOnly = outputLengthOnlyElement.checked;

  // 获取布尔-是否合并近似长度
  var ifMergeSimilarLengthElement = document.getElementById("ifMergeSimilarLength");
  var ifMergeSimilarLength = ifMergeSimilarLengthElement.checked;

  // 获取布尔-是否插入创建时间戳
  var creationTimeStampElement = document.getElementById("ifInsertCreationTimeStamp");
  var ifInsertCreationTimeStamp = creationTimeStampElement.checked;

  // 将宽幅和制图比例传递给函数
  csInterface.evalScript(
    "calculateArea(" +
      materialWidth +
      ", " +
      drawingScale +
      ", " +
      dataAccuracy +
      ", " +
      isAddExtraText +
      ", " +
      isAlertResult +
      ", " +
      ifOutputLengthOnly +
      ", " +
      ifMergeSimilarLength +
      ", " +
      ifInsertCreationTimeStamp +
      ")"
  );
}
